package com.itany.nmms.service.impl;

import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.ProductTypeMapper;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.entity.ProductTypeExample;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/28 9:07
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class ProductTypeServiceImpl implements ProductTypeService {
    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException {
        ProductTypeMapper typeMapper = (ProductTypeMapper) ObjectFactory.getObject("typeMapper");
        if(ParameterUtil.isNull(name)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        // 商品类型的名称不能重复
        // 判断当前商品类型的名称是否已经存在
        // 根据类型名称查询类型信息
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andNameEqualTo(name);
        List<ProductType> types = typeMapper.selectByExample(example);
        if(!types.isEmpty()){
            // 如果查询到了数据,则表示已经存在了
            throw new ProductTypeExistException("该商品类型已经存在");
        }

        // 开始添加
        ProductType type = new ProductType();
        type.setName(name);
        // 默认设置新增的商品类型状态为:启用
        type.setStatus(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        typeMapper.insertSelective(type);
    }

    @Override
    public List<ProductType> findAll() {
        ProductTypeMapper typeMapper = (ProductTypeMapper) ObjectFactory.getObject("typeMapper");
        List<ProductType> types = typeMapper.selectByExample(new ProductTypeExample());
        return types;
    }

    @Override
    public List<ProductType> findEnable() {
        ProductTypeMapper typeMapper = (ProductTypeMapper) ObjectFactory.getObject("typeMapper");
        ProductTypeExample example = new ProductTypeExample();
        example.or()
                .andStatusEqualTo(StatusConstant.PRODUCT_TYPE_STATUS_ENABLED);
        List<ProductType> types = typeMapper.selectByExample(example);
        return types;
    }
}
